package com.hepinghospital.user.ui.activity.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.base.BaseApplication;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.AddressResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收货地址
 * Created by HP on 2020/2/20.
 */

public class MyAddressActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_add_family)
    ImageView btnAddFamily;
    @BindView(R.id.cl_empty)
    ConstraintLayout clEmpty;
    @BindView(R.id.iv_empty)
    ImageView ivEmpty;
    @BindView(R.id.tv_empty_tip)
    TextView tvEmptyTip;

    MyAdapter adapter;

    List<AddressResponse> list = new ArrayList<>();
    private boolean isFromMine;

    @Override
    protected void initViews() {
        tvTitle.setText("收货地址");
        btnAddFamily.setVisibility(View.VISIBLE);
        LinearLayoutManager manager = new LinearLayoutManager(MyAddressActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getAddressList());
    }

    @Override
    protected void initData() {
//        getAddressList();
        isFromMine = getIntent().getBooleanExtra("isFromMine", false);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_address;
    }


    @OnClick(R.id.btn_add_family)
    void btn_add_family() {
        Intent intent = new Intent(this, AddAddressActivity.class);
//        intent.putExtra( "isAdd", true );
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(MyAddressActivity.this).inflate(R.layout.item_address, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                if (isFromMine) {
//                        Bundle bundle = new Bundle();
//                        bundle.putSerializable( "obj", list.get( position ) );
//                        startActivity( AddAddressActivity.class, bundle );
                } else {
                    Intent mIntent = new Intent();
                    mIntent.putExtra("obj", list.get(position));
                    setResult(RESULT_OK, mIntent);
                    finish();
                }

            });
            holder.btn_edt.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putSerializable("obj", list.get(position));
                startActivity(AddAddressActivity.class, bundle);
            });

            holder.tv_name.setText(list.get(position).consigneeName);
            holder.tv_phone.setText(list.get(position).consigneePhone);
            holder.tv_addr.setText(list.get(position).addr);
            if ("0".equals(list.get(position).isDefault)) {

                holder.tv_is_default.setVisibility(View.GONE);
            } else if ("1".equals(list.get(position).isDefault)) {
                holder.tv_is_default.setVisibility(View.VISIBLE);
            }
//            holder.btn_edt.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("obj", list.get(position));
//                    startActivity(AddMyFamilyActivity.class,bundle);
//                }
//            });
//            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
//            holder.tv_price.setText(doctorList.get(position).fee);
//            holder.tv_turn_num.setText(doctorList.get(position).num);
//            holder.tv_name.setText(doctorList.get(position).docName);
//            ImageLoaderHelper.getInstance().GlideImageLoader(MyFamilyActivity.this,doctorList.get(position).);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_phone, tv_addr, tv_is_default;
            ImageView btn_edt;

            public Holder(View itemView) {
                super(itemView);
                tv_addr = itemView.findViewById(R.id.tv_addr);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_phone = itemView.findViewById(R.id.tv_phone);
                tv_is_default = itemView.findViewById(R.id.tv_is_default);
                btn_edt = itemView.findViewById(R.id.btn_edt);
            }
        }
    }


    private void getAddressList() {
        RestProxy.getInstance().getAddressList(BaseApplication.currentUserId, new BaseObserver<List<AddressResponse>>() {

            @Override
            public void onSuccess(List<AddressResponse> result) {
                if (result.isEmpty()) {
                    clEmpty.setVisibility(View.VISIBLE);
                    refreshLayout.setVisibility(View.GONE);
                    ivEmpty.setBackgroundResource(R.drawable.icon_empty_address);
                    tvEmptyTip.setText("您还没有收获地址信息哦～\n" +
                            "       赶快添加一个吧！");
                } else {
                    clEmpty.setVisibility(View.GONE);
                    refreshLayout.setVisibility(View.VISIBLE);
                    list.clear();
                    list.addAll(result);
                    adapter.notifyDataSetChanged();
                    refreshLayout.finishRefresh(true);
                }
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(MyAddressActivity.this, e);
            }
        });
    }
}
